package cz.tomasdvorak.cars.wicket.components.breadcrumbs;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;

import java.util.List;

public class BreadcrumbPanel extends Panel {
    public BreadcrumbPanel(String id, List<BreadcrumbItem> breadcrumbs) {
        super(id);
        add(new ListView<BreadcrumbItem>("breadcrumbs", breadcrumbs) {
            @Override
            protected void populateItem(final ListItem<BreadcrumbItem> breadcrumbItemListItem) {
                final BreadcrumbItem obj = breadcrumbItemListItem.getModelObject();
                final Link link = new Link("link") {
                    @Override
                    public void onClick() {
                        if (obj.getPageInstance() != null) {
                            setResponsePage(obj.getPageInstance());
                        } else if (obj.getPage() != null) {
                            setResponsePage(obj.getPage());
                        } else {
                            // TODO: what to do?
                        }
                    }
                };

                link.setEnabled(obj.getPage() != null || obj.getPageInstance() != null);
                link.add(new Label("label", obj.getTitle()));
                breadcrumbItemListItem.add(link);
            }
        });
    }
}
